from bs4 import BeautifulSoup
import sys
sys.path.append('..\kernel')
sys.path.append('..\model')
import spider_main
import city_model
# 抓取
class CityController(object):
    def __init__(self):
        #实例化核心对象
        self.spider = spider_main.SpiderMain()
        self.city_model = city_model.CityModel()
    def index(self):
        chengshi=dict()
        url='http://baike.baidu.com/link?url=lbLSrzBkY4pM6TXfoP1r4zYf2RbMfg4BQuFguRP0s7DPBz44DJM6C0BZs5mhEu7D1bSHNBV2QtQK46MmtYwQDHU3py36Uk4L7PyhDveFFODJADNpB2UWuvUuyGsIa828r5iD8bFUJrs7sPx_D7rjUVm8w_vPobvCf_p1EME6KTfX2JsgGmA08Tf9ddjzzrYt'

        # 下载页面
        html_cont = self.spider.downloader.downloader(url)
        html_obj=BeautifulSoup(html_cont, 'html.parser', from_encoding='utf-8')
        # 开始节点,从这个开始节点开始往下遍历获取所有的省份和城市名单
        #start_node=html_obj.find('div',class_='level-3',attrs={'label-module':'para-title'})
        capital_list=html_obj.find_all('div',class_='level-3',attrs={'label-module':'para-title'})
        #chengshi[start_node.find('h3').contents[-1]]=''
        for title in capital_list:
            try:
                province=title.find('h3').contents[-1]
                for name in self.cehngshi(title):
                    exists_data = self.city_model.isCity_exists(name)
                    if(exists_data==0):
                        self.city_model.add_city(name, province)
                        print(name)
                # chengshi[title.find('h3').contents[-1]]=self.cehngshi(title) #省会名称:省线下面的城市
            except:
                print(title)
        exit()

    def cehngshi(self,capital):
        sibling_array=list()
        for sibling in capital.next_siblings:
            if sibling.name=='div' and sibling.get('class')[0]=='para':
                for cs in sibling.strings:
                    if cs=='[1]':
                        continue
                    cs_list=cs.split()  # 用空格将字符串分割成list
                    sibling_array+=cs_list # 合并列表
            if sibling.name=='div' and sibling.get('class')[0]=='para-title':
                break
        return sibling_array

if __name__=='__main__':
    obj=CityController()
    obj.index()